package pkg461project;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.InputMismatchException;

/**
 *
 * @author ahall1
 */
public class Main {

    private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) {
        System.out.println("Welcome to the Mars Reality, Inc. Management Sytem!\n");
        processMenu();
        System.out.println("Thank you for using my program, have a nice day!\n");
    }

    public static void processMenu() {
        int input = 0;
        boolean exit = false;
        while (!exit) {
            try {
                System.out.print("Please select an option from the following menu by entering\n"
                        + "a number cooresponding to your desired action:\n"
                        + "1: Enter new information to the database\n"
                        + "2: Update information already in the database\n"
                        + "3: Delete information already in the database\n"
                        + "4: View information in the database\n"
                        + "5: Exit program\n"
                        + "Enter Choice: ");
                input = Integer.parseInt(in.readLine());
                System.out.println();
                switch (input) {
                    case 1:
                        databaseInsert();
                        break;
                    case 2:
                        databaseUpdate();
                        break;
                    case 3:
                        break;
                    case 4:
                        databaseView();
                        break;
                    case 5:
                        return;
                    default:
                        throw new InvalidMenuChoiceException("Not a valid menu choice, please enter an integer cooresponding to your choice");
                }
            } catch (InputMismatchException e) {
                System.out.println("Invalid entry for given field.\n");
            } catch (InvalidMenuChoiceException e) {
                System.out.println(e.getMessage());
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.exit(1);
            }
        }
    }

    private static void databaseInsert() {
        DatabaseInsert di;
        int input = 0;
        int num = 0;
        int employee_num = 0;
        int client_num = 0;
        int property_num = 0;
        String choice;
        String temp;
        String comments;
        Date viewDate;
        ArrayList<String> telephone_nums = new ArrayList<String>();
        ArrayList<String> fax_nums = new ArrayList<String>();
        while (true) {
            try {
                System.out.print("Select what type of object to add to the database:\n"
                        + "1. Branch\n"
                        + "2. Employee at a branch\n"
                        + "3. Client\n"
                        + "4. Property Owner\n"
                        + "5. Property\n"
                        + "6. Lease between client and property\n"
                        + "7. Property advertised in a newspaper\n"
                        + "8. Property viewing\n"
                        + "9. Go back to previous menu\n"
                        + "Enter Choice: ");
                input = Integer.parseInt(in.readLine());
                System.out.println();
                switch (input) {
                    case 1:
                        Branch branch = new Branch();
                        System.out.println("Enter the ID of the manager for this branch:");
                        branch.setManager_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the street address the branch is located on:");
                        branch.setStreet(in.readLine());
                        System.out.println("Enter the city the branch is located in:");
                        branch.setCity(in.readLine());
                        System.out.println("Enter the state the branch is located in:");
                        branch.setState(in.readLine());
                        System.out.println("Enter the zipcode the branch is located in:");
                        branch.setZipcode(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the telephone number of the branch:");
                        branch.setTelephone_number(in.readLine());
                        System.out.println("Enter the fax number of the branch:");
                        branch.setFax_number(in.readLine());
                        di = new DatabaseInsert();
                        num = di.insertBranch(branch);
                        di.close();
                        System.out.println("The ID assigned to the branch is " + num + "\n");
                        break;
                    case 2:
                        Employee employee = new Employee();
                        System.out.println("Enter the branch number the employee works at:");
                        num = Integer.parseInt(in.readLine());
                        System.out.println("Enter the name of the employee:");
                        employee.setName(in.readLine());
                        System.out.println("Enter the sex of the employee:");
                        employee.setSex(in.readLine());
                        System.out.println("Enter the street the employee lives on:");
                        employee.setStreet(in.readLine());
                        System.out.println("Enter the city the employee lives in:");
                        employee.setCity(in.readLine());
                        System.out.println("Enter the state the employee lives in:");
                        employee.setState(in.readLine());
                        System.out.println("Enter the zipcode the employee lives in:");
                        employee.setZipcode(Integer.parseInt(in.readLine()));
                        di = new DatabaseInsert();
                        num = di.insertEmployee(employee, num);
                        di.close();
                        System.out.println("The ID assigned to the new employee is " + num);
                        break;
                    case 3:
                        Client client = new Client();
                        System.out.println("Enter the name of the client:");
                        client.setName(in.readLine());
                        System.out.println("Enter the max rent the client is willing to pay:");
                        client.setMax_rent(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the any preferred accomodations the client wants:");
                        client.setPreferred_accomodation(in.readLine());
                        System.out.println("Enter the client's home number:");
                        client.setHome_number(in.readLine());
                        System.out.println("Enter the client's work number:");
                        client.setWork_number(in.readLine());
                        di = new DatabaseInsert();
                        num = di.insertClient(client);
                        di.close();
                        System.out.println("The ID assigned to the client is " + num);
                        break;
                    case 4:
                        System.out.println("Does the property own the property for a business? y/n");
                        choice = in.readLine();
                        if (choice.equals("y")) {
                            Business_owner businessOwner = new Business_owner();
                            System.out.println("Enter the name of the business owner:");
                            businessOwner.setName(in.readLine());
                            System.out.println("Enter the type of business:");
                            businessOwner.setBusiness_type(in.readLine());
                            System.out.println("Enter a contact name:");
                            businessOwner.setContact_name(in.readLine());
                            System.out.println("Enter telephone numbers delimited by a new line and type END when completed:");
                            temp = in.readLine();
                            while (!temp.equals("END")) {
                                telephone_nums.add(new String(temp));
                            }
                            businessOwner.setTelephone_numbers(telephone_nums);
                            System.out.println("Enter fax numbers delimited by a new line and type END when completed:");
                            temp = in.readLine();
                            while (!temp.equals("END")) {
                                fax_nums.add(new String(temp));
                            }
                            businessOwner.setFax_numbers(fax_nums);
                            di = new DatabaseInsert();
                            num = di.insertBusinessOwner(businessOwner);
                            di.close();
                            System.out.println("The ID assigned to the client is " + num);
                        } else if (choice.equals("n")) {
                            Private_owner privateOwner = new Private_owner();
                            System.out.println("Enter the name of the owner:");
                            privateOwner.setName(in.readLine());
                            System.out.println("Enter telephone numbers delimited by a new line and type END when completed:");
                            temp = in.readLine();
                            while (!temp.equals("END")) {
                                telephone_nums.add(new String(temp));
                            }
                            privateOwner.setTelephone_numbers(telephone_nums);
                            System.out.println("Enter fax numbers delimited by a new line and type END when completed:");
                            temp = in.readLine();
                            while (!temp.equals("END")) {
                                fax_nums.add(new String(temp));
                            }
                            privateOwner.setFax_numbers(fax_nums);
                            di = new DatabaseInsert();
                            num = di.insertPrivateOwner(privateOwner);
                            di.close();
                            System.out.println("The ID assigned to the client is " + num);
                        } else {
                            throw new InvalidMenuChoiceException("Not a valid menu choice, "
                                    + "please enter y or n");
                        }
                        break;
                    case 5:
                        Property property = new Property();
                        System.out.println("Enter the type of property:");
                        property.setProperty_type(in.readLine());
                        System.out.println("Enter the number of bedrooms:");
                        property.setNum_bedrooms(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the number of square feet:");
                        property.setSquare_footage(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the date of the last update in the format YYYY-MM-DD:");
                        property.setLast_update(Date.valueOf(in.readLine()));
                        System.out.println("Enter the monthly rent:");
                        property.setMonthly_rent(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the property owner:");
                        property.setProperty_owner(in.readLine());
                        System.out.println("Enter the minimum price:");
                        property.setMin_price(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the maximum price:");
                        property.setMax_price(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the monthly management fee:");
                        property.setMonthly_management_fee(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the street the property is on:");
                        property.setStreet(in.readLine());
                        System.out.println("Enter the city the property is in:");
                        property.setCity(in.readLine());
                        System.out.println("Enter the state the property is in:");
                        property.setState(in.readLine());
                        System.out.println("Enter the zipcode the property is in:");
                        property.setZipcode(Integer.parseInt(in.readLine()));
                        di = new DatabaseInsert();
                        num = di.insertProperty(property);
                        di.close();
                        System.out.println("The ID assigned to the property is " + num);
                        break;
                    case 6:
                        Lease lease = new Lease();
                        System.out.println("Enter the number of the employee that completed the lease:");
                        employee_num = Integer.parseInt(in.readLine());
                        System.out.println("Enter the number of the client that viewed the lease:");
                        client_num = Integer.parseInt(in.readLine());
                        System.out.println("Enter the property number of the lease:");
                        lease.setProperty_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the start date of the lease in the format YYYY-MM-DD: ");
                        lease.setStart_date(Date.valueOf(in.readLine()));
                        System.out.println("Enter the finish date of the lease in the format YYYY-MM-DD: ");
                        lease.setFinish_date(Date.valueOf(in.readLine()));
                        System.out.println("Enter the payment method for the lease:");
                        lease.setPayment_method(in.readLine());
                        System.out.println("Enter the duration of the lease in months:");
                        lease.setDuration(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the deposit amount for the lease:");
                        lease.setDeposit_amount(Integer.parseInt(in.readLine()));
                        di = new DatabaseInsert();
                        num = di.insertLease(lease, employee_num, client_num);
                        di.close();
                        System.out.println("The ID assigned to the property is " + num);
                        break;
                    case 7:
                        Newspaper_ad newsAd = new Newspaper_ad();
                        System.out.println("Enter the name of the newspaper:");
                        newsAd.setNewspaper_name(in.readLine());
                        System.out.println("Enter the property id of the ad:");
                        newsAd.setProperty_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the cost of the ad:");
                        newsAd.setCost_of_ad(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the date of the ad in the format YYYY-MM-DD:");
                        newsAd.setDate_of_ad(Date.valueOf(in.readLine()));
                        System.out.println("Enter the street where the newspaper company is:");
                        newsAd.setStreet(in.readLine());
                        System.out.println("Enter the city where the newspaper company is:");
                        newsAd.setCity(in.readLine());
                        System.out.println("Enter the state where the newspaper company is:");
                        newsAd.setState(in.readLine());
                        System.out.println("Enter the zipcode where the newspaper company is:");
                        newsAd.setZipcode(Integer.parseInt(in.readLine()));
                        di = new DatabaseInsert();
                        num = di.insertNewspaperAd(newsAd);
                        di.close();
                        System.out.println("The ID assigned to the property is " + num);
                        break;
                    case 8:
                        System.out.println("Enter the ID of the property:");
                        property_num = Integer.parseInt(in.readLine());
                        System.out.println("Enter the ID of the client:");
                        client_num = Integer.parseInt(in.readLine());
                        System.out.println("Enter the date of the viewing in the format YYYY-MM-DD:");
                        viewDate = Date.valueOf(in.readLine());
                        System.out.println("Enter comments from the client about the viewing");
                        comments = new String(in.readLine());
                        di = new DatabaseInsert();
                        di.insertView(property_num, client_num, viewDate, comments);
                        di.close();
                        System.out.println("The viewing data has been saved");
                        break;
                    case 9:
                        return;
                    default:
                        throw new InvalidMenuChoiceException("Not a valid menu choice, "
                                + "please enter an integer cooresponding to your choice");
                }
            } catch (ClassNotFoundException e) {
                System.out.println("\nThere was an error connecting to the database\n"
                        + "Please restart the program or try again later.");
            } catch (SQLException e) {
                System.out.println("\nThere was an error connecting to the database\n"
                        + "Please restart the program or try again later.");
            } catch (InvalidMenuChoiceException e) {
                System.out.println(e.getMessage());
            } catch (InputMismatchException e) {
                System.out.println("Invalid entry for given field.\n");
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.exit(1);
            }
        }
    }

    public static void databaseUpdate() {
        DatabaseUpdate du;
        int num;
        String choice;
        while (true) {
            int input;
            try {
                System.out.print("Select what type of object to add to the database:\n"
                        + "1. Branch\n"
                        + "2. Employee at a branch\n"
                        + "3. Client\n"
                        + "4. Property Owner\n"
                        + "5. Property\n"
                        + "6. Lease between client and property\n"
                        + "7. Property advertised in a newspaper\n"
                        + "8. Go back to previous menu\n"
                        + "Enter Choice: ");
                input = Integer.parseInt(in.readLine());
                System.out.println();
                switch (input) {
                    case 1:
                        Branch branch = new Branch();
                        System.out.println("Enter the ID of the branch to be updated:");
                        branch.setBranch_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the ID of the manager for this branch:");
                        branch.setManager_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the street address the branch is located on:");
                        branch.setStreet(in.readLine());
                        System.out.println("Enter the city the branch is located in:");
                        branch.setCity(in.readLine());
                        System.out.println("Enter the state the branch is located in:");
                        branch.setState(in.readLine());
                        System.out.println("Enter the zipcode the branch is located in:");
                        branch.setZipcode(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the telephone number of the branch:");
                        branch.setTelephone_number(in.readLine());
                        System.out.println("Enter the fax number of the branch:");
                        branch.setFax_number(in.readLine());
                        du = new DatabaseUpdate();
                        du.updateBranch(branch);
                        du.close();
                        break;
                    case 2:
                        Employee employee = new Employee();
                        System.out.println("Enter the branch number the employee works at:");
                        num = Integer.parseInt(in.readLine());
                        System.out.println("Enter the name of the employee:");
                        employee.setName(in.readLine());
                        System.out.println("Enter the sex of the employee:");
                        employee.setSex(in.readLine());
                        System.out.println("Enter the street the employee lives on:");
                        employee.setStreet(in.readLine());
                        System.out.println("Enter the city the employee lives in:");
                        employee.setCity(in.readLine());
                        System.out.println("Enter the state the employee lives in:");
                        employee.setState(in.readLine());
                        System.out.println("Enter the zipcode the employee lives in:");
                        employee.setZipcode(Integer.parseInt(in.readLine()));
                        du = new DatabaseUpdate();
                        du.updateEmployee(employee, num);
                        du.close();
                        break;
                    case 3:
                        Lease lease = new Lease();
                        System.out.println("Enter the ID of the lease:");
                        lease.setLease_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the property number of the lease:");
                        lease.setProperty_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the start date of the lease in the format YYYY-MM-DD: ");
                        lease.setStart_date(Date.valueOf(in.readLine()));
                        System.out.println("Enter the finish date of the lease in the format YYYY-MM-DD: ");
                        lease.setFinish_date(Date.valueOf(in.readLine()));
                        System.out.println("Enter the payment method for the lease:");
                        lease.setPayment_method(in.readLine());
                        System.out.println("Enter the duration of the lease in months:");
                        lease.setDuration(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the deposit amount for the lease:");
                        lease.setDeposit_amount(Integer.parseInt(in.readLine()));
                        du = new DatabaseUpdate();
                        du.updateLease(lease);
                        du.close();
                        break;
                    case 4:
                        Newspaper_ad newsAd = new Newspaper_ad();
                        System.out.println("Enter the ID for the newspaper ad:");
                        newsAd.setNewspaper_ad_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the name of the newspaper:");
                        newsAd.setNewspaper_name(in.readLine());
                        System.out.println("Enter the property id of the ad:");
                        newsAd.setProperty_num(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the cost of the ad:");
                        newsAd.setCost_of_ad(Integer.parseInt(in.readLine()));
                        System.out.println("Enter the date of the ad in the format YYYY-MM-DD:");
                        newsAd.setDate_of_ad(Date.valueOf(in.readLine()));
                        System.out.println("Enter the street where the newspaper company is:");
                        newsAd.setStreet(in.readLine());
                        System.out.println("Enter the city where the newspaper company is:");
                        newsAd.setCity(in.readLine());
                        System.out.println("Enter the state where the newspaper company is:");
                        newsAd.setState(in.readLine());
                        System.out.println("Enter the zipcode where the newspaper company is:");
                        newsAd.setZipcode(Integer.parseInt(in.readLine()));
                        du = new DatabaseUpdate();
                        du.updateNewspaperAd(newsAd);
                        du.close();
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                    case 8:
                        return;
                    default:
                        throw new InvalidMenuChoiceException("Not a valid menu choice, "
                                + "please enter an integer cooresponding to your choice");
                }
            } catch (ClassNotFoundException e) {
                System.out.println("\nThere was an error connecting to the database\n"
                        + "Please restart the program or try again later.");
            } catch (SQLException e) {
                System.out.println("\nThere was an error connecting to the database\n"
                        + "Please restart the program or try again later.");
            } catch (InvalidMenuChoiceException e) {
                System.out.println(e.getMessage());
            } catch (InputMismatchException e) {
                System.out.println("Invalid entry for given field.\n");
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.exit(1);
            }
        }
    }

    private static void databaseView() throws SQLException, ClassNotFoundException {
        DatabaseView dv = new DatabaseView();
        while (true) {
            int input;
            try {
                System.out.print("Select what information you would like to view:\n"
                        + "1. Branch by City\n"
                        + "2. Num brances in a State\n"
                        + "23. Return to previous menu\n"
                        + "Enter Choice: ");
                input = Integer.parseInt(in.readLine());
                System.out.println();
                switch (input) {
                    case 1:
                        System.out.println("Enter the city to find branches in: ");
                        String city = in.readLine();
                        dv.searchCityBranch(city);
                        dv.close();
                    case 2:
                        System.out.println("Enter the state to find the number of branches:");
                        String state = in.readLine();
                        dv.searchStateBranches(state);
                        dv.close();
                    case 23:
                        return;
                    default:
                        throw new InvalidMenuChoiceException("Not a valid menu choice, "
                                + "please enter an integer cooresponding to your choice");   
                }
            } 
            catch (InvalidMenuChoiceException e) {
                System.out.println(e.getMessage());
            }catch (InputMismatchException e) {
                System.out.println("Invalid entry for given field.\n");
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.exit(1);
            }
        }
    }
}
